/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.tables;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import database.DBManager;
import database.objects.DOMap;
import database.objects.DOObstacle;
import java.net.UnknownHostException;
import java.util.List;

/**
 *
 * @author Tri
 */
public class DTMap {
    private DB m_db;
    public DTMap() {
        m_db = DBManager.getInstance().getDB();
    }
    
    public DOMap getMap(String mapID) {
        DOMap map = null;
        BasicDBObject object = new BasicDBObject();
        object.put(DOMap.KEY_MAP_ID, mapID);
        DBCursor cursor = getCollection().find(object);
        if(cursor.count() > 0) {
            BasicDBObject queryResult = (BasicDBObject)cursor.curr();
            int speed = queryResult.getInt(DOMap.KEY_SPEED);
            int level = queryResult.getInt(DOMap.KEY_LEVEL);
            int completeTime = queryResult.getInt(DOMap.KEY_COMPLETE_TIME);
            int completeSteps; queryResult.getInt(DOMap.KEY_COMPLETE_STEP);
            String matrix = queryResult.getString(DOMap.KEY_MATRIX);
            int wonMoney = queryResult.getInt(DOMap.KEY_WON_MONEY);
            BasicDBList list = (BasicDBList)cursor.curr().get(DOMap.KEY_OBSTACLE_LIST);
            
            String[] subObstacle = new String[list.size()];
            int i = 0;
            for (Object tempObject : list) {
                BasicDBObject stringObj = (BasicDBObject)tempObject;
                String retStr = stringObj.getString(DOMap.KEY_SUB_OBSTACLE);
                subObstacle[i] = retStr;
                i++;
            }
            map = new DOMap(mapID, speed, level, completeTime, completeTime, matrix, wonMoney, subObstacle);
        }
        return map;
    }
    
    public void insertMap(DOMap map) {
       BasicDBObject dbObject = new BasicDBObject();
       dbObject.put(DOMap.KEY_MAP_ID, map.getMapID());
       dbObject.put(DOMap.KEY_LEVEL, map.getLevel());
       dbObject.put(DOMap.KEY_SPEED, map.getSpeed());
       dbObject.put(DOMap.KEY_WON_MONEY, map.getWonMoney());
       dbObject.put(DOMap.KEY_MATRIX, map.getMatrix());
       dbObject.put(DOMap.KEY_COMPLETE_STEP, map.getCompleteSteps());
       dbObject.put(DOMap.KEY_COMPLETE_TIME, map.getCompleteTime());
       for (String s : map.getObstaclesList()) {
           BasicDBObject object = new BasicDBObject();
           object.put(DOMap.KEY_SUB_OBSTACLE, s);
           dbObject.put(DOMap.KEY_OBSTACLE_LIST, object);
       }
       getCollection().insert(dbObject);
    }
    
    public void deleteMap(String mapID) {
        BasicDBObject dbObject = new BasicDBObject();
        dbObject.put(DOMap.KEY_MAP_ID, mapID);
        getCollection().remove(dbObject);
    }
    
    public void deleteAll() {
        DBCursor cursor = getCollection().find();
        while(cursor.hasNext()) {
            getCollection().remove(cursor.next());
        }
    }
    
    public void updateMap(DOMap map) {
        BasicDBObject dbObject = new BasicDBObject();
        dbObject.put(DOMap.KEY_LEVEL, map.getLevel());
        dbObject.put(DOMap.KEY_SPEED, map.getSpeed());
        dbObject.put(DOMap.KEY_WON_MONEY, map.getWonMoney());
        dbObject.put(DOMap.KEY_MATRIX, map.getMatrix());
        dbObject.put(DOMap.KEY_COMPLETE_STEP, map.getCompleteSteps());
        dbObject.put(DOMap.KEY_COMPLETE_TIME, map.getCompleteTime());
        BasicDBObject searchQuery = new BasicDBObject().append(DOMap.KEY_MAP_ID, map.getMapID());
        getCollection().update(searchQuery, dbObject);
        
    }
    
    public DBCollection getCollection() {
        return m_db.getCollection("Map");
    }
}
